class ThongKesController < ApplicationController

  def index
    respond_to do  |format|
      format.html
    end
  end

  def tk_phong
    @lts = LoaiT.all
    if params[:phongID] == nil
      params[:phongID] = 1
      params[:tinhtrang] = "all"
      params[:loai] = ""
    end
    @ctp = CtPhong.where( :phong_id => params[:phongID])
    if params[:loai] != ""
      @ct = @ctp.joins(:tai_san).where("tai_sans.LoaiT_id = ?", params[:loai])
    else
      @ct = @ctp
    end
    if params[:tinhtrang] != "all"
      @ct = @ct.where("tinh_trang Like ?", params[:tinhtrang] )
    end
    @quanli = QuanLy.where( :phong_id => params[:phongID] ).first()

    respond_to do |format|
      format.html
      format.xls
    end
  end

  def tk_nhanvien
      #  thong tin chung
    @nhanvien = NhanVien.all
    if params[:nhanvienID] == nil
      params[:nhanvienID] = 1
    end
      # danh sach phong nv quan li
    @quanli = QuanLy.where(:nhan_vien_id => params[:nhanvienID])
      # danh sach tai san nv so huu
    @sohuu  = CtPhong.where(:nguoi_sd_id => params[:nhanvienID])
      # danh sach phieu nhap
    @pnhap = PhieuNhap.where(:nhan_vien_id => params[:nhanvienID])

    respond_to do |format|
      format.html
      format.xls
    end
  end

  def tk_taisan
    if params[:taisanID] == nil
      params[:taisanID] = 1
    end
    @ctp = CtPhong.where(:tai_san_id => params[:taisanID])
    @pn = PhieuNhap.where(:tai_san_id => params[:taisanID])
    respond_to do |format|
      format.html
      format.json {render json: @ctp}
      format.xls
    end
  end
  def tk_loaitaisan
    @loai = LoaiT.find(params[:loaiid])
    @ctp = CtPhong.joins(:tai_san).where("tai_sans.LoaiT_id = ?", params[:loaiid])
    respond_to do |format|
      format.xls
      format.json {render json: @ctp}
    end
  end

  # Tk_taisan
  def getLoai
    @loai = LoaiT.all
    respond_to do |format|
      format.json {render json: @loai}
    end
  end
  def getTaiSan
    @ts = TaiSan.all
    respond_to do |format|
      format.json {render json: @ts}
    end
  end
  def getHoatDong
    @nk = NhatKy.getDSHD(params[:tsid],params[:chon],params[:ngaychon],params[:ngaychon2])
    respond_to do |format|
      format.json {render json: @nk}
    end
  end
  def getPhieuNhapTS
    @pn = PhieuNhap.getAllbyTS(params[:tsid])
    respond_to do |format|
      format.json {render json: @pn}
    end
  end
  #Thong ke phong
  def getPhong
    @p = Phong.all
    respond_to do |format|
      format.json {render json: @p}
    end
  end
  def getCtPhong
    if params[:phongid] != nil
      @ctp = CtPhong.getAllbyPhong(params[:phongid],params[:loaiid],'')
      if params[:tinhtrang] != ''
        @ctp = CtPhong.getAllbyPhong(params[:phongid],params[:loaiid],params[:tinhtrang])
      end
    elsif params[:tsid] != nil
      @ctp = CtPhong.getAllbyTS(params[:tsid])
    elsif params[:nvid] != nil
      @ctp = CtPhong.getAllbyQL(params[:nvid])
    elsif params[:loaiid] != nil
      @ctp = CtPhong.getAllbyLoai(params[:loaiid])
    end
    respond_to do |format|
      format.json {render json: @ctp}
    end
  end
  def getDSLoai
    @dsl = CtPhong.getSLLoai(params[:phongid])
    respond_to do |format|
      format.json {render json: @dsl}
    end
  end
  #thong ke nhan vien
  def getNhanVien
    @nv = NhanVien.all
    respond_to do |format|
      format.json {render json: @nv}
    end
  end
  def getPhieuNhap
    @pn = PhieuNhap.getAllbyNV(params[:nvid])
    respond_to do |format|
      format.json {render json: @pn}
    end
  end
  def getQuanli
    if params[:nvid] != nil
      @ql = QuanLy.getAllbyNV(params[:nvid])
    elsif params[:pid] != nil
      @ql = QuanLy.getAllbyPhong(params[:pid])
    end
    respond_to do |format|
      format.json {render json: @ql}
    end
  end
end
